package com.jumper.property.comparer.diff.getter;

import com.jumper.property.comparer.core.bean.PropertySetting;
import com.jumper.property.comparer.diff.DefaultDiffResolver;
import com.jumper.property.comparer.diff.Element;
import com.jumper.property.comparer.diff.value.ObjectDiff;
import com.jumper.property.comparer.core.result.DiffResult;
import com.jumper.property.comparer.util.ValueUtil;

import java.lang.reflect.Field;

public class ObjectDiffGetter extends AbstractDiffGetter<PropertySetting> {

    @Override
    public void visit(Element<PropertySetting> element) {
        Field field = element.getSetting().getField();
        Class<?> fieldType = field.getType();
        Object beforeValue = ValueUtil.getValue(element.getBeforeValue(), field);
        Object afterValue = ValueUtil.getValue(element.getAfterValue(), field);
        DiffResult rst = DefaultDiffResolver.RESOLVER.getDifference(fieldType, beforeValue, afterValue);
        if (rst.isHasDiff()) {
            ObjectDiff diff = new ObjectDiff(element.getSetting(), beforeValue, afterValue, rst.getDiffValList());
            addDiffs(diff.getDiffValues());
        }
    }
}
